package com.wryyyyy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wryyyyy.dto.AllPositionDTO;
import com.wryyyyy.dto.CountryFoulContent;
import com.wryyyyy.dto.Result;
import com.wryyyyy.entity.*;
import com.wryyyyy.mapper.CountryMapper;
import com.wryyyyy.mapper.FoulFactoryMapper;
import com.wryyyyy.mapper.PressureMapper;
import com.wryyyyy.mapper.SourceMapper;
import com.wryyyyy.real.ICirculatingWaterDao;
import com.wryyyyy.real.IFoulWaterDao;
import com.wryyyyy.real.IPureWaterDao;
import com.wryyyyy.service.ICountryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wryyyyy
 * @since 2024-05-14
 */
@Slf4j
@Service
public class CountryServiceImpl extends ServiceImpl<CountryMapper, Country> implements ICountryService {
    @Resource
    private CountryMapper countryMapper;

    @Resource
    private SourceMapper sourceMapper;

    @Resource
    private PressureMapper pressureMapper;

    @Resource
    private FoulFactoryMapper foulFactoryMapper;

    @Resource
    private IFoulWaterDao foulWaterDao;

    @Resource
    private IPureWaterDao pureWaterDao;

    @Resource
    private ICirculatingWaterDao circulatingWaterDao;

    @Override
    public Result getPassingRate(List<String> countryIds) {
        List<String> factoryIds = foulFactoryMapper.getFactoryByCountry(countryIds);
        Double foulPassing = foulWaterDao.getPassingRate(factoryIds, false);
        Double purePassing = pureWaterDao.getPassingRate(countryIds);
        Double circulatingPassing = circulatingWaterDao.getPassingRate(countryIds);
        Map<String, Double> passing = new HashMap<>();
        passing.put("foulPassing", foulPassing);
        passing.put("purePassing", purePassing);
        passing.put("circulatingPassing", circulatingPassing);
        return Result.ok(passing);
    }

    @Override
    public Result getFoulContent(List<String> countryIds) {
        List<String> factoryIds = foulFactoryMapper.getFactoryByCountry(countryIds);
        FoulWaterQuality content = foulWaterDao.getContent(factoryIds);
        return Result.ok(content);
    }

    @Override
    public Result getPureContent(List<String> countryIds) {
        PureWaterQuality content = pureWaterDao.getContent(countryIds);
        return Result.ok(content);
    }

    @Override
    public Result getCirculatingContent(List<String> countryIds) {
        CirculatingWaterQuality content = circulatingWaterDao.getContent(countryIds);
        return Result.ok(content);
    }

    @Override
    public Result getFoulAmount(List<String> countryIds) {
        List<String> factoryIds = foulFactoryMapper.getFactoryByCountry(countryIds);
        Map<LocalDateTime, Double> amount = foulWaterDao.getAmount(factoryIds);
        return Result.ok(amount);
    }

    @Override
    public Result getPureAmount(List<String> countryIds) {
        Map<LocalDateTime, Double> amount = pureWaterDao.getAmount(countryIds);
        return Result.ok(amount);
    }

    @Override
    public Result getCirculatingAmount(List<String> countryIds) {
        Map<LocalDateTime, Double> amount = circulatingWaterDao.getAmount(countryIds);
        return Result.ok(amount);
    }

    @Override
    public Result getRanking(List<String> countryIds) {
        QueryWrapper<Country> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", countryIds);
        List<Country> countries = countryMapper.selectList(queryWrapper);
        return Result.ok(countries);
    }

    @Override
    public Result getBasicInfo(List<String> countryIds) {
        Map<String, Object> result = new HashMap<>();

        Integer population = countryMapper.getPopulationSum(countryIds);
        AllPositionDTO allPositionDTO = new AllPositionDTO();
        QueryWrapper<Source> sourceQueryWrapper = new QueryWrapper<>();
        sourceQueryWrapper.in("country_id", countryIds);
        List<Source> sources = sourceMapper.selectList(sourceQueryWrapper);
        allPositionDTO.setSources(sources);
        QueryWrapper<FoulFactory> foulQueryWrapper = new QueryWrapper<>();
        foulQueryWrapper.in("country_id", countryIds);
        List<FoulFactory> foulFactories = foulFactoryMapper.selectList(foulQueryWrapper);
        allPositionDTO.setFoulFactories(foulFactories);
        QueryWrapper<Pressure> pressuresQueryWrapper = new QueryWrapper<>();
        sourceQueryWrapper.in("country_id", countryIds);
        List<Pressure> pressures = pressureMapper.selectList(pressuresQueryWrapper);
        allPositionDTO.setPressures(pressures);

        result.put("population", population);
        result.put("position", allPositionDTO);

        return Result.ok(result);
    }

    @Override
    public Result getEachPassingRate(List<String> countryIds) {
        Map<String, Double> result = new HashMap<>();
        for (String countryId : countryIds) {
            List<String> factoryIds = foulFactoryMapper.getFactoryByOneCountry(countryId);
            Double foulPassing = foulWaterDao.getPassingRate(factoryIds, false);
            result.put(countryId, foulPassing);
        }
        return Result.ok(result);
    }

    @Override
    public Result getOverview(List<String> countryIds, Boolean week) {
        Map<String, Double> result = new HashMap<>();

        List<String> factoryIds = foulFactoryMapper.getFactoryByCountry(countryIds);

        Double foulPassing = foulWaterDao.getPassingRate(factoryIds, week);
        result.put("passing", foulPassing);
        Double foulTotalAmount = foulWaterDao.getTotalAmount(factoryIds, week);
        result.put("totalAmount", foulTotalAmount);
        Double dosage = foulWaterDao.getTotalDosage(factoryIds, week);
        result.put("dosage", dosage);

        return Result.ok(result);
    }

    @Override
    public Result getEachContent(List<String> countryIds) {
        List<CountryFoulContent> result = new ArrayList<>();

        for (String countryId : countryIds) {
            List<String> factoryIds = foulFactoryMapper.getFactoryByOneCountry(countryId);
            FoulWaterQuality foulWaterQuality = foulWaterDao.getContent(factoryIds);
            result.add(new CountryFoulContent(countryId, foulWaterQuality));
        }

        return Result.ok(result);
    }

    @Override
    public Result getFoulDosage(List<String> countryIds) {
        List<String> factoryIds = foulFactoryMapper.getFactoryByCountry(countryIds);
        Map<LocalDateTime, Double> dosage = foulWaterDao.getDosage(factoryIds);
        return Result.ok(dosage);
    }
}